/*
 * Twanoh Library, a useful Android utility library.
 * Copyright (C) 2006 Richard Schilling
 * 
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

package gnu.org.twanoh.app;

import static gnu.org.twanoh.app.ActivityState.ON_CREATE;
import static gnu.org.twanoh.app.ActivityState.ON_DESTROY;
import static gnu.org.twanoh.app.ActivityState.ON_PAUSE;
import static gnu.org.twanoh.app.ActivityState.ON_RESTART;
import static gnu.org.twanoh.app.ActivityState.ON_RESUME;
import static gnu.org.twanoh.app.ActivityState.ON_START;
import static gnu.org.twanoh.app.ActivityState.ON_STOP;
import gnu.org.twanoh.statemachine.StateMachine;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.res.Configuration;
import android.content.res.Resources.Theme;
import android.os.Bundle;
import android.util.AttributeSet;
import android.util.Log;
import android.view.ContextMenu;
import android.view.ContextMenu.ContextMenuInfo;
import android.view.MenuItem;
import android.view.View;

/**
 * This activity contains some basic functionality that validates how the
 * lifecycle callbacks are managed. While the Android API dictates that certain
 * methods are called in a specific order (e.g. onCreate(), onResume(), etc.),
 * there are cases where it's helpful to know when a particular device calls the
 * other functions in an activity. This can help isolate issues around phone
 * specific implementations.
 * <p>
 * It's generally assumed that an implementation of Android follows the rules in
 * the API. But, this activity allows us to validate that this is in fact the
 * case for a particular phone.
 * <p>
 * This activity has as little overhead as possilbe, but it is not meant to be
 * used in a production application. Rather, it is meant to be used during
 * development to discover how a particular device manages the lifecycle of an
 * Activity.
 * 
 * @author rschilling
 */
public class LifecycleValidatorActivity extends Activity {

    /**
     * Constant used for logcat output.
     */
    public static final String LOG_TAG = "LifecycleValidatorActivity";

    private StateMachine<ActivityState> stateMachine;

    public LifecycleValidatorActivity() {
        super();
        stateMachine = new StateMachine<ActivityState>(ActivityStateMapFactory.getStateMap(),
                ActivityState.PRE_CREATE);

    }

    /**
     * Dumps the current state along with the specified text to logcat.
     * 
     * @param text the text to output.
     */
    private void logState(String text) {
        Log.d(LOG_TAG, "state: " + stateMachine.currentState().toString() + ", " + text
                + ", class: " + getClass().getName());
    }

    @Override
    public boolean isFinishing() {
        logState("isFinishing");
        return super.isFinishing();

    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        logState("onActivityResult");
    }

    @Override
    protected void onApplyThemeResource(Theme theme, int resid, boolean first) {

        super.onApplyThemeResource(theme, resid, first);
        logState("onApplyThemeResource");
    }

    @Override
    protected void onChildTitleChanged(Activity childActivity, CharSequence title) {

        super.onChildTitleChanged(childActivity, title);
        logState("onChildTitleChanged");
    }

    @Override
    public void onConfigurationChanged(Configuration newConfig) {

        super.onConfigurationChanged(newConfig);
        logState("onConfigurationChanged");
    }

    @Override
    public void onContentChanged() {

        super.onContentChanged();
        logState("onContentChanged");
    }

    @Override
    public boolean onContextItemSelected(MenuItem item) {
        logState("onContextItemSelected");
        return super.onContextItemSelected(item);

    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        stateMachine.transition(ON_CREATE);
        logState("onCreate");

    }

    @Override
    public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) {

        super.onCreateContextMenu(menu, v, menuInfo);
        logState("onCreateContextMenu");
    }

    @Override
    public CharSequence onCreateDescription() {
        logState("onCreateDescription");

        return super.onCreateDescription();
    }

    @Override
    public View onCreateView(String name, Context context, AttributeSet attrs) {
        logState("onCreateView");
        return super.onCreateView(name, context, attrs);
    }

    @Override
    protected void onDestroy() {

        super.onDestroy();
        stateMachine.transition(ON_DESTROY);

        logState("onDestroy");
    }

    @Override
    protected void onNewIntent(Intent intent) {
        super.onNewIntent(intent);
        logState("onNewIntent");
    }

    @Override
    protected void onPause() {

        super.onPause();
        stateMachine.transition(ON_PAUSE);
        logState("onPause");

    }

    @Override
    protected void onPostCreate(Bundle savedInstanceState) {

        super.onPostCreate(savedInstanceState);
        logState("onPostCreate");
    }

    @Override
    protected void onPostResume() {

        super.onPostResume();
        logState("onPostResume");
    }

    @Override
    protected void onRestart() {

        super.onRestart();
        stateMachine.transition(ON_RESTART);
        logState("onRestart");
    }

    @Override
    protected void onRestoreInstanceState(Bundle savedInstanceState) {

        super.onRestoreInstanceState(savedInstanceState);
        logState("onRestoreInstanceState");
    }

    @Override
    protected void onResume() {
        super.onResume();
        stateMachine.transition(ON_RESUME);
        logState("onResume");

    }

    @Override
    protected void onSaveInstanceState(Bundle outState) {

        super.onSaveInstanceState(outState);
        logState("onSaveInstanceState");
    }

    @Override
    protected void onStart() {
        super.onStart();
        stateMachine.transition(ON_START);
        logState("onStart");

    }

    @Override
    protected void onStop() {
        super.onStop();

        stateMachine.transition(ON_STOP);
        logState("onStop");
    }

    @Override
    protected void onTitleChanged(CharSequence title, int color) {

        super.onTitleChanged(title, color);
        logState("onTitleChanged");
    }

    @Override
    protected void onUserLeaveHint() {

        super.onUserLeaveHint();
        logState("onUserLeaveHint");
    }

    @Override
    protected void attachBaseContext(Context newBase) {

        super.attachBaseContext(newBase);
        logState("attachBaseContext");
    }

    @Override
    public void setTheme(int resid) {

        super.setTheme(resid);
        logState("setTheme");
    }

}
